﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Entities;

namespace Iacym.Repository
{
    public class TipoReunionRepository : MasterRepository, ITipoReunionRepository
    {
        public List<TIPO_REUNION> GetAllFromTipoReunion()
        {
            var query = from p in context.TIPO_REUNION
                        select p;

            return query.ToList();
        }

        public List<TIPO_REUNION> GetFromTipoReunionByCriterio(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.TIPO_REUNION
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;

                return query.ToList();
            }
            else
            {
                var query = from p in context.TIPO_REUNION
                            select p;

                return query.ToList();
            }
        }

        public TIPO_REUNION GetFromTipoReunionById(int id)
        {
            var query = from p in context.TIPO_REUNION
                        where p.Id.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddTipoReunion(TIPO_REUNION tipoReunion)
        {
            context.TIPO_REUNION.Add(tipoReunion);
            context.SaveChanges();
        }

        public void UpdateTipoReunion(TIPO_REUNION tipoReunion)
        {
            context.TIPO_REUNION.Attach(tipoReunion);
            context.Entry(tipoReunion).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveTipoReunion(int id)
        {
            var existe = context.TIPO_REUNION.Where(p => p.Id.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                context.TIPO_REUNION.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExistDuplicidadDescripcion(string descripcion, int id)
        {
            var query = from c in context.TIPO_REUNION
                        where c.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 && c.Id != id
                        select c;

            return query.Any();
        }
    }
}
